//
//  KongWSelfSegment.m
//  LoveOfPet
//
//  Created by dllo on 15/11/18.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "KongWSelfSegment.h"
#import "KongWAreaViewController.h"
#define kHeight     [UIScreen mainScreen].bounds.size.height / 667
#define kWidth      [UIScreen mainScreen].bounds.size.width / 375

@interface KongWSelfSegment ()<UITableViewDataSource, UITableViewDelegate, AreaDelegate>

@end
@implementation KongWSelfSegment
-(void)dealloc
{
    [_kindOfJob release];
    [_area release];
    [_kindOfJobView release];
    [_kindTable release];
    [_kindArray release];
    [_recommend release];
    [_recommendView release];
    [_recommendTable release];
    [_recommendArray release];
    
    [super dealloc];
}

-(void)creatHeadView
{
    
    self.area = [UIButton buttonWithType:UIButtonTypeCustom];
    self.area.frame = CGRectMake(0, 64, (self.frame.size.width - kWidth * 2) / 3, kHeight * 30);
    [self.area setTitle:@"地区" forState:UIControlStateNormal];
    [self.area setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.area addTarget:self action:@selector(areaController) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.area];
    //分割线1
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake((self.frame.size.width - kWidth * 2) / 3, self.area.frame.origin.y, kWidth * 1, self.area.frame.size.height)];
    line1.backgroundColor = [UIColor grayColor];
    [self addSubview:line1];
    [line1 release];
    
    self.kindOfJobView = [[UIView alloc] initWithFrame:CGRectMake(self.area.frame.origin.x + self.area.frame.size.width + 1, 64, (self.frame.size.width - kWidth * 2) / 3,  self.area.frame.size.height)];
    self.kindOfJobView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.kindOfJobView];
    [self.kindOfJobView release];
    
    
    self.kindOfJob = [UIButton buttonWithType:UIButtonTypeCustom];
    self.kindOfJob.frame = CGRectMake(self.area.frame.origin.x + self.area.frame.size.width + 1, 64, (self.frame.size.width - kWidth * 2) / 3,  self.area.frame.size.height);
    [self.kindOfJob setTitle:@"全部" forState:UIControlStateNormal];
    [self.kindOfJob setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.kindOfJob addTarget:self action:@selector(kindOfJob:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.kindOfJob];
    
    
    
    //分割线2
    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(self.kindOfJob.frame.origin.x + self.kindOfJob.frame.size.width, self.kindOfJob.frame.origin.y, kWidth * 1, self.kindOfJob.frame.size.height)];
    line2.backgroundColor = [UIColor grayColor];
    [self addSubview:line2];
    [line2 release];
    self.recommendView = [[UIView alloc] initWithFrame:CGRectMake(((self.frame.size.width - kWidth * 2) / 3 + 1) * 2, 64, (self.frame.size.width - kWidth * 2) / 3, kHeight * 30)];
    self.recommendView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.recommendView];
    [self.recommendView release];
    
    self.recommend = [UIButton buttonWithType:UIButtonTypeCustom];
    self.recommend.frame = CGRectMake(self.kindOfJob.frame.origin.x + self.kindOfJob.frame.size.width + 1, 64, (self.frame.size.width - kWidth * 2) / 3, self.kindOfJob.frame.size.height);
    [self.recommend setTitle:@"智能推荐" forState:UIControlStateNormal];
    [self.recommend setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.recommend addTarget:self action:@selector(recommend:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.recommend];
    
    
    //分割线3
    UILabel *line3 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.area.frame.origin.y + self.area.frame.size.height, self.frame.size.width, 1)];
    line3.backgroundColor = [UIColor grayColor];
    [self addSubview:line3];
    [line3 release];
    
}

-(void)creatKindTable
{
    self.kindTable = [[UITableView alloc] initWithFrame:self.kindOfJobView.bounds style:UITableViewStylePlain];
    
    self.kindTable.delegate = self;
    self.kindTable.dataSource = self;
    [self.kindTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"doctorKindTableCellIndentify"];
    [self.kindOfJobView addSubview:self.kindTable];
    [self.kindTable release];
}

-(void)creatRecommendTable
{
    self.recommendTable = [[UITableView alloc] initWithFrame:self.recommendView.bounds style:UITableViewStylePlain];
    
    self.recommendTable.delegate = self;
    self.recommendTable.dataSource = self;
    [self.recommendTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"doctorRecommendTableCellIndentify"];
    [self.recommendView addSubview:self.recommendTable];
    [self.recommendTable release];
}

-(void)areaController
{
    [self.kindOfJob setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.recommend setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.area setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.area.backgroundColor = [UIColor colorWithRed:0.0806 green:0.3724 blue:0.9363 alpha:1.0];
    self.kindOfJob.backgroundColor = [UIColor whiteColor];
    self.recommend.backgroundColor = [UIColor whiteColor];
    KongWAreaViewController *area = [[KongWAreaViewController alloc] init];
    area.delegate = self;
//    [self.navigationController pushViewController:area animated:YES];
    [area release];
}

-(void)kindOfJob:(UIButton *)sender
{
    [self.recommend setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.area setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor colorWithRed:0.0806 green:0.3724 blue:0.9363 alpha:1.0];
    self.recommend.backgroundColor = [UIColor whiteColor];
    self.area.backgroundColor = [UIColor whiteColor];
    sender.selected = !sender.isSelected;
    
    if (sender.isSelected) {
        [self pushKindOfJobView];
        [self creatKindTable];
        [self backRecommendView];
        
        
    }else
    {
        [self backKindOfJobView];
        
    }
    self.recommend.selected = NO;
}

-(void)recommend:(UIButton *)sender
{
    [self.kindOfJob setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.area setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor colorWithRed:0.0806 green:0.3724 blue:0.9363 alpha:1.0];
    self.kindOfJob.backgroundColor = [UIColor whiteColor];
    self.area.backgroundColor = [UIColor whiteColor];
    sender.selected = !sender.isSelected;
    if (sender.isSelected) {
        
        [self pushRecommendView];
        [self creatRecommendTable];
        [self backKindOfJobView];
        
    }else
    {
        [self backRecommendView];
        
    }
    self.kindOfJob.selected = NO;
}
#pragma mark 动画炒作
-(void)pushKindOfJobView
{
    [UIView animateWithDuration:0.1 animations:^{
        self.kindOfJobView.frame = CGRectMake(0, 64 + kHeight * 31, self.frame.size.width, kHeight * 200);
    }];
}

-(void)backKindOfJobView
{
    [UIView animateWithDuration:0.1 animations:^{
        self.kindOfJobView.frame = CGRectMake((self.frame.size.width - kWidth * 2) / 3 + 1, 64, (self.frame.size.width - kWidth * 2) / 3,  kHeight * 30);
    }];
    self.kindTable.hidden = YES;
    
}

-(void)pushRecommendView
{
    [UIView animateWithDuration:0.1 animations:^{
        self.recommendView.frame = CGRectMake(0, 64 + kHeight * 31, self.frame.size.width, kHeight * 120);
    }];
}

-(void)backRecommendView
{
    [UIView animateWithDuration:0.1 animations:^{
        self.recommendView.frame = CGRectMake(((self.frame.size.width - kWidth * 2) / 3 + 1) * 2, 64, (self.frame.size.width - kWidth * 2) / 3, kHeight * 30);
    }];
    self.recommendTable.hidden = YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
